<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */

namespace App\Service;

use GuzzleHttp\Exception\GuzzleException;

class NotificationService
{
    /**
     * @param string $text
     * @return void
     * @throws GuzzleException
     */
    public function sendTelegramMsg(string $text): void
    {
        try {
            $botToken = env('TELEGRAM_BOT_TOKEN');
            $chatId = env('TELEGRAM_CHAT_ID');

            $url = "https://api.telegram.org/bot{$botToken}/sendMessage";
            guzzle()->post($url, [
                'form_params' => [
                    'chat_id' => $chatId,
                    'text' => $text,
                ],
            ]);
        } catch (\Throwable $e) {
            logger('log')->error('发送 Telegram 通知失败: ' . $e->getMessage());
        }
    }

    /**
     * @throws GuzzleException
     */
    public function sendDingTalkMsg(string $content)
    {
        $webhook = env('DINGTALK_WEBHOOK');
        guzzle()->post($webhook, [
            'json' => [
                'msgtype' => 'text',
                'text' => ['content' => $content],
            ],
        ]);
    }
}
